package com.esri.android.map;

import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.symbol.FillSymbol;
import com.esri.core.symbol.MarkerSymbol;
import com.esri.core.symbol.PictureMarkerSymbol;
import com.github.mikephil.charting.utils.Utils;
import com.sangfor.ssl.service.utils.IGeneral;

/* loaded from: classes.dex */
public class LocationDisplayManager {
    a a;

    native String nativeGetAccuracySymbol(long j);

    native int nativeGetAlpha(long j);

    native int nativeGetAutoPanMode(long j);

    native String nativeGetCourseSymbol(long j);

    native String nativeGetDefaultSymbol(long j);

    native String nativeGetHeadingSymbol(long j);

    native float nativeGetNavigationPointHeightFactor(long j);

    native String nativeGetPingSymbol(long j);

    native boolean nativeGetShowsAccuracy(long j);

    native boolean nativeGetShowsLocation(long j);

    native boolean nativeGetShowsPing(long j);

    native float nativeGetWanderExtentFactor(long j);

    native boolean nativeIsSymbolFound(long j, float f, float f2, int i);

    native void nativeLocationDisplayManagerInitialize(long j, String str, String str2, String str3, int i, float f, float f2, int i2, boolean z, boolean z2, boolean z3);

    native void nativeSetAccuracySymbol(long j, String str);

    native void nativeSetAlpha(long j, int i);

    native void nativeSetAutoPanMode(long j, int i);

    native void nativeSetCourseSymbol(long j, String str);

    native void nativeSetDefaultSymbol(long j, String str);

    native void nativeSetHeadingSymbol(long j, String str);

    native void nativeSetNavigationPointHeightFactor(long j, float f);

    native void nativeSetPingSymbol(long j, String str);

    native void nativeSetShowsAccuracy(long j, boolean z);

    native void nativeSetShowsLocation(long j, boolean z);

    native void nativeSetShowsPing(long j, boolean z);

    native void nativeSetWanderExtentFactor(long j, float f);

    native void nativeStop(long j);

    native void nativeUpdateHeading(long j, float f);

    native void nativeUpdateLocation(long j, double d, double d2, double d3, double d4, double d5);

    /* loaded from: classes.dex */
    public enum AutoPanMode {
        OFF(0),
        LOCATION(1),
        NAVIGATION(2),
        COMPASS(3);
        
        private final int a;

        AutoPanMode(int i) {
            this.a = i;
        }

        public int getValue() {
            return this.a;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public LocationDisplayManager(MapView mapView) {
        this.a = new a(mapView);
    }

    public void start() {
        this.a.a();
    }

    public void pause() {
        this.a.c();
    }

    public void resume() {
        this.a.b();
    }

    public void stop() {
        this.a.d();
    }

    public Location getLocation() {
        return this.a.e();
    }

    public Point getPoint() {
        return this.a.f();
    }

    public boolean isStarted() {
        return this.a.m();
    }

    public AutoPanMode getAutoPanMode() {
        return this.a.n();
    }

    public LocationListener getLocationListener() {
        return this.a.o();
    }

    public void setLocationListener(LocationListener locationListener) {
        this.a.a(locationListener);
    }

    public void setAutoPanMode(AutoPanMode autoPanMode) {
        this.a.a(autoPanMode);
    }

    public boolean isSymbolFound(float f, float f2, int i) {
        return this.a.a(f, f2, i);
    }

    public void setLocationAcquiringSymbol(MarkerSymbol markerSymbol) throws Exception {
        this.a.a(markerSymbol);
    }

    public MarkerSymbol getLocationAcquiringSymbol() throws Exception {
        return this.a.g();
    }

    public void setDefaultSymbol(MarkerSymbol markerSymbol) throws Exception {
        this.a.b(markerSymbol);
    }

    public MarkerSymbol getDefaultSymbol() throws Exception {
        return this.a.h();
    }

    public void setCourseSymbol(MarkerSymbol markerSymbol) throws Exception {
        this.a.c(markerSymbol);
    }

    public MarkerSymbol getCourseSymbol() throws Exception {
        return this.a.i();
    }

    public void setHeadingSymbol(MarkerSymbol markerSymbol) throws Exception {
        this.a.d(markerSymbol);
    }

    public MarkerSymbol getHeadingSymbol() throws Exception {
        return this.a.j();
    }

    public void setAccuracySymbol(FillSymbol fillSymbol) throws Exception {
        this.a.a(fillSymbol);
    }

    public FillSymbol getAccuracySymbol() throws Exception {
        return this.a.k();
    }

    public void setPingSymbol(MarkerSymbol markerSymbol) throws Exception {
        this.a.e(markerSymbol);
    }

    public MarkerSymbol getPingSymbol() throws Exception {
        return this.a.l();
    }

    public boolean isAllowNetworkLocation() {
        return this.a.p();
    }

    public void setAllowNetworkLocation(boolean z) {
        this.a.a(z);
    }

    public boolean isAccuracyCircleOn() {
        return this.a.r();
    }

    public void setAccuracyCircleOn(boolean z) {
        this.a.c(z);
    }

    public void setShowPings(boolean z) {
        this.a.b(z);
    }

    public boolean isShowPings() {
        return this.a.q();
    }

    public void setShowLocation(boolean z) {
        this.a.d(z);
    }

    public boolean isShowLocation() {
        return this.a.s();
    }

    public void setUseCourseSymbolOnMovement(boolean z) {
        this.a.e(z);
    }

    public boolean isUseCourseSymbolOnMovement() {
        return this.a.t();
    }

    public void setOpacity(float f) {
        this.a.a(f);
    }

    public float getOpacity() {
        return this.a.u();
    }

    public void setWanderExtentFactor(float f) {
        this.a.b(f);
    }

    public float getWanderExtentFactor() {
        return this.a.v();
    }

    public void setNavigationPointHeightFactor(float f) {
        this.a.c(f);
    }

    public float getNavigationPointHeightFactor() {
        return this.a.w();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class a implements SensorEventListener {
        private static final String J = "sdk_gps_location.png";
        private static final String K = "sdk_gps_locationgrey.png";
        private static final String L = "sdk_gps_navigation.png";
        private static final String M = "sdk_gps_compass.png";
        private static final String N = "mdpi";
        private static final String O = "hdpi";
        private static final String P = "xhdpi";
        private static final String Q = "xxhdpi";
        private static final String R = "xxxhdpi";
        private static final int S = 480;
        private static final int c = 0;
        private static final int d = 1;
        private float F;
        private MapView I;
        private MarkerSymbol e;
        private MarkerSymbol f;
        private MarkerSymbol g;
        private MarkerSymbol h;
        private FillSymbol i;
        private MarkerSymbol j;
        private Location n;
        private Location o;
        private float p;
        private LocationManager q;
        private SensorManager r;
        private Sensor s;
        private Sensor t;
        private boolean k = true;
        private int l = 100;
        private int m = 0;
        private boolean u = false;
        private boolean v = false;
        private boolean w = false;
        private boolean x = true;
        private boolean y = true;
        private boolean z = true;
        private boolean A = true;
        private AutoPanMode B = AutoPanMode.OFF;
        private float C = 1.0f;
        private float D = 0.5f;
        private float E = 0.125f;
        private LocationListener G = null;
        private C0011a H = null;
        private float[] T = new float[3];
        private float[] U = new float[3];
        private float[] V = new float[9];
        private float[] W = new float[9];
        private float[] X = new float[3];
        final float a = 57.29578f;

        private float a(float f, float f2, float f3) {
            return f < f2 ? f2 : f > f3 ? f3 : f;
        }

        private float b(int i) {
            return 255.0f / i;
        }

        private int d(float f) {
            return (int) (f * 255.0f);
        }

        @Override // android.hardware.SensorEventListener
        public void onAccuracyChanged(Sensor sensor, int i) {
        }

        public a(MapView mapView) {
            this.q = null;
            this.r = null;
            this.I = mapView;
            if (this.q == null) {
                this.q = (LocationManager) mapView.getContext().getSystemService("location");
            }
            if (this.r == null) {
                SensorManager sensorManager = (SensorManager) mapView.getContext().getSystemService("sensor");
                this.r = sensorManager;
                this.s = sensorManager.getDefaultSensor(1);
                this.t = this.r.getDefaultSensor(2);
            }
            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((WindowManager) this.I.getContext().getSystemService("window")).getDefaultDisplay().getMetrics(displayMetrics);
            StringBuilder sb = new StringBuilder("/com/esri/android/map/res/");
            if (displayMetrics.densityDpi <= 160) {
                sb.append(N);
            } else if (displayMetrics.densityDpi <= 240) {
                sb.append(O);
            } else if (displayMetrics.densityDpi <= 320) {
                sb.append(P);
            } else if (displayMetrics.densityDpi <= S) {
                sb.append(Q);
            } else {
                sb.append(R);
            }
            sb.append("/");
            String sb2 = sb.toString();
            Class<?> cls = LocationDisplayManager.this.getClass();
            this.e = new PictureMarkerSymbol(Drawable.createFromStream(cls.getResourceAsStream(sb2 + K), null));
            Class<?> cls2 = LocationDisplayManager.this.getClass();
            this.f = new PictureMarkerSymbol(Drawable.createFromStream(cls2.getResourceAsStream(sb2 + J), null));
            Class<?> cls3 = LocationDisplayManager.this.getClass();
            this.g = new PictureMarkerSymbol(Drawable.createFromStream(cls3.getResourceAsStream(sb2 + L), null));
            Class<?> cls4 = LocationDisplayManager.this.getClass();
            this.h = new PictureMarkerSymbol(Drawable.createFromStream(cls4.getResourceAsStream(sb2 + M), null));
        }

        public void a() {
            MapView mapView = this.I;
            if (mapView == null || mapView.isRecycled() || m()) {
                return;
            }
            if (!this.I.isLoaded()) {
                this.I.j = true;
                return;
            }
            try {
                if (!this.w) {
                    try {
                        y();
                    } catch (Exception unused) {
                        Log.e(com.esri.core.internal.a.a, "There was a problem initializing the GPS. One or more of the symbols could not be serialized to JSON");
                        return;
                    }
                }
                if (this.H == null) {
                    this.H = new C0011a();
                }
                this.v = true;
                if (this.k) {
                    a(1);
                }
                a(0);
                if (this.B != AutoPanMode.COMPASS || this.u) {
                    return;
                }
                z();
            } catch (NullPointerException e) {
                MapView mapView2 = this.I;
                if (mapView2 != null && !mapView2.isRecycled()) {
                    throw e;
                }
            }
        }

        private void y() throws Exception {
            String json = this.e.toJson();
            LocationDisplayManager.this.nativeLocationDisplayManagerInitialize(this.I.a.l, json, json, json, this.B.getValue(), this.E, this.D, d(this.C), this.x, this.y, this.z);
            if (this.i != null) {
                LocationDisplayManager.this.nativeSetAccuracySymbol(this.I.a.l, this.i.toJson());
            }
            if (this.j != null) {
                LocationDisplayManager.this.nativeSetPingSymbol(this.I.a.l, this.j.toJson());
            }
            this.w = true;
        }

        public void b() {
            if (m()) {
                if (this.k) {
                    a(1);
                }
                a(0);
                if (this.r == null || this.B != AutoPanMode.COMPASS || this.u) {
                    return;
                }
                z();
            }
        }

        public void c() {
            if (m()) {
                LocationManager locationManager = this.q;
                if (locationManager != null) {
                    locationManager.removeUpdates(this.H);
                }
                if (this.r != null && this.u) {
                    A();
                }
                LocationDisplayManager.this.nativeStop(this.I.a.l);
            }
        }

        public void d() {
            if (m()) {
                LocationManager locationManager = this.q;
                if (locationManager != null) {
                    locationManager.removeUpdates(this.H);
                }
                if (this.r != null && this.u) {
                    A();
                }
                LocationDisplayManager.this.nativeStop(this.I.a.l);
                this.v = false;
            }
        }

        void a(int i) {
            String str = i == 1 ? IGeneral.LOG_TAG_NETWORK : GeocodeSearch.GPS;
            Location lastKnownLocation = this.q.getLastKnownLocation(str);
            this.o = lastKnownLocation;
            if (lastKnownLocation != null) {
                lastKnownLocation.setSpeed(0.0f);
                this.o.setBearing(0.0f);
                a(this.o);
                LocationListener locationListener = this.G;
                if (locationListener != null) {
                    locationListener.onLocationChanged(this.o);
                }
            }
            if (this.q.getAllProviders().contains(str)) {
                this.q.requestLocationUpdates(str, this.l, this.m, this.H);
            }
        }

        public Location e() {
            if (this.n != null) {
                return new Location(this.n);
            }
            if (this.o != null) {
                return new Location(this.o);
            }
            return null;
        }

        public Point f() {
            Location e = e();
            if (e == null) {
                return null;
            }
            Point point = new Point(e.getLongitude(), e.getLatitude());
            return (this.I.getSpatialReference() == null || 4326 == this.I.getSpatialReference().getID()) ? point : GeometryEngine.project(point.getX(), point.getY(), this.I.getSpatialReference());
        }

        boolean a(float f, float f2, int i) {
            if (this.w) {
                return LocationDisplayManager.this.nativeIsSymbolFound(this.I.a.l, f, f2, i);
            }
            return false;
        }

        public void a(MarkerSymbol markerSymbol) {
            if (markerSymbol == null) {
                return;
            }
            this.e = markerSymbol;
        }

        public MarkerSymbol g() {
            return this.e;
        }

        public void b(MarkerSymbol markerSymbol) throws Exception {
            if (markerSymbol == null) {
                return;
            }
            if (this.w) {
                LocationDisplayManager.this.nativeSetDefaultSymbol(this.I.a.l, markerSymbol.toJson());
            } else {
                this.f = markerSymbol;
            }
        }

        public MarkerSymbol h() throws Exception {
            if (this.w) {
                return MarkerSymbol.fromJson(LocationDisplayManager.this.nativeGetDefaultSymbol(this.I.a.l));
            }
            return this.f;
        }

        public void c(MarkerSymbol markerSymbol) throws Exception {
            if (markerSymbol == null) {
                return;
            }
            if (this.w) {
                LocationDisplayManager.this.nativeSetCourseSymbol(this.I.a.l, markerSymbol.toJson());
            } else {
                this.g = markerSymbol;
            }
        }

        public MarkerSymbol i() throws Exception {
            if (this.w) {
                return MarkerSymbol.fromJson(LocationDisplayManager.this.nativeGetCourseSymbol(this.I.a.l));
            }
            return this.g;
        }

        public void d(MarkerSymbol markerSymbol) throws Exception {
            if (markerSymbol == null) {
                return;
            }
            if (this.w) {
                LocationDisplayManager.this.nativeSetHeadingSymbol(this.I.a.l, markerSymbol.toJson());
            } else {
                this.h = markerSymbol;
            }
        }

        public MarkerSymbol j() throws Exception {
            if (this.w) {
                return MarkerSymbol.fromJson(LocationDisplayManager.this.nativeGetHeadingSymbol(this.I.a.l));
            }
            return this.h;
        }

        public void a(FillSymbol fillSymbol) throws Exception {
            if (fillSymbol == null) {
                return;
            }
            if (this.w) {
                LocationDisplayManager.this.nativeSetAccuracySymbol(this.I.a.l, fillSymbol.toJson());
            } else {
                this.i = fillSymbol;
            }
        }

        public FillSymbol k() throws Exception {
            if (this.w) {
                return FillSymbol.fromJson(LocationDisplayManager.this.nativeGetAccuracySymbol(this.I.a.l));
            }
            return this.i;
        }

        public void e(MarkerSymbol markerSymbol) throws Exception {
            if (markerSymbol == null) {
                return;
            }
            if (this.w) {
                LocationDisplayManager.this.nativeSetPingSymbol(this.I.a.l, markerSymbol.toJson());
            } else {
                this.j = markerSymbol;
            }
        }

        public MarkerSymbol l() throws Exception {
            if (this.w) {
                return MarkerSymbol.fromJson(LocationDisplayManager.this.nativeGetPingSymbol(this.I.a.l));
            }
            return this.j;
        }

        public boolean m() {
            return this.v;
        }

        public AutoPanMode n() {
            if (this.w) {
                return AutoPanMode.values()[LocationDisplayManager.this.nativeGetAutoPanMode(this.I.a.l)];
            }
            return this.B;
        }

        public LocationListener o() {
            return this.G;
        }

        public void a(LocationListener locationListener) {
            this.G = locationListener;
        }

        public void a(AutoPanMode autoPanMode) {
            this.B = autoPanMode;
            if (this.w) {
                LocationDisplayManager.this.nativeSetAutoPanMode(this.I.a.l, this.B.getValue());
                if (autoPanMode != AutoPanMode.COMPASS || this.u) {
                    return;
                }
                z();
            }
        }

        private void z() {
            this.r.registerListener(this, this.s, 2);
            this.r.registerListener(this, this.t, 2);
            this.u = true;
        }

        private void A() {
            this.r.unregisterListener(this);
            this.u = false;
        }

        public boolean p() {
            return this.k;
        }

        public void a(boolean z) {
            this.k = z;
        }

        public void b(boolean z) {
            this.x = z;
            if (this.w) {
                LocationDisplayManager.this.nativeSetShowsPing(this.I.a.l, this.x);
            }
        }

        public boolean q() {
            if (this.w) {
                return LocationDisplayManager.this.nativeGetShowsPing(this.I.a.l);
            }
            return this.x;
        }

        public void c(boolean z) {
            this.y = z;
            if (this.w) {
                LocationDisplayManager.this.nativeSetShowsAccuracy(this.I.a.l, this.y);
            }
        }

        public boolean r() {
            if (this.w) {
                return LocationDisplayManager.this.nativeGetShowsAccuracy(this.I.a.l);
            }
            return this.y;
        }

        public void d(boolean z) {
            this.z = z;
            if (this.w) {
                LocationDisplayManager.this.nativeSetShowsLocation(this.I.a.l, this.z);
            }
        }

        public boolean s() {
            if (this.w) {
                return LocationDisplayManager.this.nativeGetShowsLocation(this.I.a.l);
            }
            return this.z;
        }

        public void e(boolean z) {
            this.A = z;
        }

        public boolean t() {
            return this.A;
        }

        public void a(float f) {
            this.C = a(f, 0.0f, 1.0f);
            if (this.w) {
                LocationDisplayManager.this.nativeSetAlpha(this.I.a.l, d(this.C));
            }
        }

        public float u() {
            if (this.w) {
                return b(LocationDisplayManager.this.nativeGetAlpha(this.I.a.l));
            }
            return this.C;
        }

        public void b(float f) {
            this.D = a(f, 0.0f, 1.0f);
            if (this.w) {
                LocationDisplayManager.this.nativeSetWanderExtentFactor(this.I.a.l, this.D);
            }
        }

        public float v() {
            if (this.w) {
                return LocationDisplayManager.this.nativeGetWanderExtentFactor(this.I.a.l);
            }
            return this.D;
        }

        public void c(float f) {
            this.E = a(f, 0.0f, 1.0f);
            if (this.w) {
                LocationDisplayManager.this.nativeSetNavigationPointHeightFactor(this.I.a.l, this.E);
            }
        }

        public float w() {
            if (this.w) {
                return LocationDisplayManager.this.nativeGetNavigationPointHeightFactor(this.I.a.l);
            }
            return this.E;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void a(Location location) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            double accuracy = location.getAccuracy();
            double bearing = location.getBearing();
            double speed = location.getSpeed();
            if (!this.A && this.B != AutoPanMode.NAVIGATION) {
                speed = Utils.DOUBLE_EPSILON;
            }
            LocationDisplayManager.this.nativeUpdateLocation(this.I.a.l, longitude, latitude, accuracy, bearing, speed);
        }

        /* JADX INFO: Access modifiers changed from: private */
        /* renamed from: com.esri.android.map.LocationDisplayManager$a$a  reason: collision with other inner class name */
        /* loaded from: classes.dex */
        public class C0011a implements LocationListener {
            private static final double b = 2.0d;

            private C0011a() {
            }

            @Override // android.location.LocationListener
            public void onLocationChanged(Location location) {
                try {
                    if (a.this.n != null) {
                        double accuracy = a.this.n.getAccuracy();
                        Double.isNaN(accuracy);
                        if (location.getAccuracy() > accuracy * b) {
                            return;
                        }
                    } else {
                        try {
                            LocationDisplayManager.this.nativeSetDefaultSymbol(a.this.I.a.l, a.this.f.toJson());
                            LocationDisplayManager.this.nativeSetCourseSymbol(a.this.I.a.l, a.this.g.toJson());
                            LocationDisplayManager.this.nativeSetHeadingSymbol(a.this.I.a.l, a.this.h.toJson());
                        } catch (Exception unused) {
                            Log.w(com.esri.core.internal.a.a, "Could not update the GPS symbols after obtaining a location fix. There was a problem serializing the symbols to JSON");
                        }
                    }
                    a.this.n = location;
                    a.this.a(location);
                    if (a.this.G != null) {
                        a.this.G.onLocationChanged(location);
                    }
                } catch (NullPointerException e) {
                    if (a.this.I != null && !a.this.I.isRecycled()) {
                        throw e;
                    }
                }
            }

            @Override // android.location.LocationListener
            public void onProviderDisabled(String str) {
                if (a.this.G != null) {
                    a.this.G.onProviderDisabled(str);
                }
            }

            @Override // android.location.LocationListener
            public void onProviderEnabled(String str) {
                a.this.a(!GeocodeSearch.GPS.equalsIgnoreCase(str) ? 1 : 0);
                if (a.this.G != null) {
                    a.this.G.onProviderEnabled(str);
                }
            }

            @Override // android.location.LocationListener
            public void onStatusChanged(String str, int i, Bundle bundle) {
                if (i == 2) {
                    a.this.a(!GeocodeSearch.GPS.equalsIgnoreCase(str) ? 1 : 0);
                }
                if (a.this.G != null) {
                    a.this.G.onStatusChanged(str, i, bundle);
                }
            }
        }

        public void x() {
            if (m()) {
                try {
                    d();
                } catch (Exception e) {
                    Log.e(com.esri.core.internal.a.a, "Can not shutdown GPS", e);
                }
            }
        }

        @Override // android.hardware.SensorEventListener
        public void onSensorChanged(SensorEvent sensorEvent) {
            float[] fArr;
            int type = sensorEvent.sensor.getType();
            if (type == 1) {
                fArr = this.T;
            } else if (type != 2) {
                return;
            } else {
                fArr = this.U;
            }
            for (int i = 0; i < 3; i++) {
                fArr[i] = sensorEvent.values[i];
            }
            if (SensorManager.getRotationMatrix(this.V, this.W, this.T, this.U)) {
                SensorManager.getOrientation(this.V, this.X);
                float f = this.X[0] * 57.29578f;
                this.F = f;
                if (f < 0.0f) {
                    this.F = f + 360.0f;
                }
                double abs = Math.abs(this.F - this.p);
                if (abs <= 7.5d || abs >= 352.5d) {
                    return;
                }
                this.p = this.F;
                LocationDisplayManager.this.nativeUpdateHeading(this.I.a.l, this.F);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a() {
        a aVar = this.a;
        if (aVar != null) {
            try {
                aVar.x();
            } catch (Exception e) {
                Log.e(com.esri.core.internal.a.a, "Can not shutdown GPS", e);
            }
        }
    }
}
